/**
 * @(#) Trasmettitore.java
 */

package it.unibo.iss.domotica.EDDomain;


import it.unibo.iss.domotica.converter.DomoticaJsonConverter;
import it.unibo.iss.domotica.edDomain.ConsumptionData;

import org.apache.log4j.Logger;

public class Transmitter extends AbstractTransmitter
{
	private Sensor sensor;
	private DomoticaJsonConverter converter;
	
	// Logger
	private Logger logger = Logger.getLogger(Transmitter.class);
	
	public Transmitter() {
		this("");
	}
	
	public Transmitter(String edName) {
		this(edName, null);
	}
	
	public Transmitter(String edName, Sensor sensor) {
		this.edName = edName;
		if(sensor != null)
			this.sensor = sensor;
		else
			this.sensor = new Sensor();
		converter = new DomoticaJsonConverter();
	}
	
//	@Override
	public void sendData(double consumption) {
//		M = "\"" + edName + "=" + consumption + "\"";
//		M = edName + "=" + consumption;
		ConsumptionData data = new ConsumptionData(edName, consumption);
		M = "\"" + converter.consumptionDataToString(data) + "\"";
		try {
			transmitterForward_consumptionData_receiver();
			logger.debug("Data sent: " + data + "(" + M + ")");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doJob() throws Exception {
		while(true) {
			sendData(sensor.getConsumption());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}
